package com.wensi.module_voice_setting

import android.widget.SeekBar
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.wensi.adapter.CommonAdapter
import com.wensi.adapter.CommonViewHolder
import com.wensi.helper.ARouterHelper
import com.wensi.lib_base.BaseActivity
import com.wensi.lib_voice.manager.VoiceManager
import com.wensi.lib_voice.tts.VoiceTTS
import com.wensi.utils.L
import kotlinx.android.synthetic.main.activity_voice_setting.*

@Route(path = ARouterHelper.PATH_VOICE_SETTING)
class VoiceSettingActivity : BaseActivity() {
    private var list: ArrayList<String> = ArrayList()
    private var indexList = emptyList<String>()
    override fun getLayoutId(): Int {
        return R.layout.activity_voice_setting
    }

    override fun getTitleText(): String {
        return getString(R.string.voice_name)
    }

    override fun isShowBack(): Boolean {
        return false
    }

    override fun initView() {
        // 设置默认值
        bar_voice_speed.progress = 5
        bar_voice_volume.progress = 5

        // 设置最大值
        bar_voice_speed.max = 15
        bar_voice_volume.max = 15

       initVoiceListener()
        btn_test.setOnClickListener {
            VoiceManager.startSpeak("嘘嘘，我开始说话了", object : VoiceTTS.OnTTSResultListener{
                override fun ttsEnd() {
                    L.i("说话已结束 - 嘘嘘我开始说话了")
                }
            })
        }
        initPeopleView()

    }
    // 初始化发音人列表
    private fun initPeopleView() {
        val ttsp = resources.getStringArray(R.array.TTSPeople)
        ttsp.forEach {
            list.add(it)
        }
        indexList = resources.getStringArray(R.array.TTSPeopleIndex).toList()

        rv_voice_people.layoutManager = LinearLayoutManager(this)
        rv_voice_people.addItemDecoration(DividerItemDecoration(this, DividerItemDecoration.VERTICAL))
        rv_voice_people.adapter = CommonAdapter(list, object : CommonAdapter.OnBindDataListener<String>{
            override fun onBindViewHolder(
                model: String,
                viewHolder: CommonViewHolder,
                type: Int,
                position: Int
            ) {
                viewHolder.setText(R.id.mTvContent, model)
                viewHolder.itemView.setOnClickListener {
                    indexList?.let {
                        VoiceManager.setPeople(it[position])
                    }

                }
            }

            override fun getLayoutId(type: Int): Int {
                return R.layout.layout_setting_voice_content
            }
        })
    }

    // 初始化 滑动监听
    private fun initVoiceListener() {
        //拖动语音速度进度条监听
        bar_voice_speed.setOnSeekBarChangeListener(object: SeekBar.OnSeekBarChangeListener{
            override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) {
                bar_voice_speed.progress = progress
                VoiceManager.setVoiceSpeed(progress.toString())
            }

            override fun onStartTrackingTouch(seekBar: SeekBar?) {
            }

            override fun onStopTrackingTouch(seekBar: SeekBar?) {
            }
        })

        //拖动语音声音大小进度条监听
        bar_voice_volume.setOnSeekBarChangeListener(object: SeekBar.OnSeekBarChangeListener{
            override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) {
                bar_voice_volume.progress = progress
                VoiceManager.setVoiceVolume(progress.toString())
            }

            override fun onStartTrackingTouch(seekBar: SeekBar?) {
            }

            override fun onStopTrackingTouch(seekBar: SeekBar?) {
            }
        })
    }
}
